from fastapi import APIRouter
from re_common.baselibrary.utils.basetime import BaseTime
from re_common.baselibrary.utils.core.requests_core import MsgCode

from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import ReturnInfo, SUCCESS, FAILED
from apps.crawler_platform.core_platform.g_model import InputPlatformModel, LobInsert, LobDelete, LobReplace
from apps.crawler_platform.util.nosqlhelper import SequoiadbHelper, NOSQLHelper
from apps.sql_app.mmongodb import Coll

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/se_lob/insert")
async def lob_test(input: InputPlatformModel[LobInsert]):
    return_info = ReturnInfo()
    table_name = input.data.table_name
    rawid = input.data.rawid
    is_replace = input.data.is_replace
    datas = input.data.datas
    db_name = Coll.col_db_dicts.get(table_name, None)
    if db_name is None:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "请在服务端代码Coll中配置集合与数据库的对应关系"
        return_info.data = ""
        return return_info.todict()
    msg_col_name = db_name + "_lob_metadata"
    msg_tab = Coll.get_table_conn(msg_col_name)
    # 两个链接都用同一个链接进行测试
    table = Coll.get_table_conn(table_name)
    dicts = await NOSQLHelper.find_one(sdb_conn=msg_tab, filter={"clname": table_name, "rawid": rawid})
    if is_replace == "True" and dicts:
        # 调用replace
        lob_id = dicts["lob_id"]
        condition = {"clname": dicts["clname"], "rawid": dicts["rawid"]}
        bools, dicts = await SequoiadbHelper.replace_lob(table, datas, lob_id, condition, msg_tab)
        return_info.status = SUCCESS
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = "lob replace 存储成功"
        return_info.data = dicts
        return return_info.todict()
    if is_replace != "True" and dicts:
        dicts["_id"] = str(dicts.get("_id", ""))
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "存在数据且不允许replace，返回失败"
        return_info.data = ""
        return return_info.todict()
    if not dicts:
        sdb_data_msg = {
            "clname": table_name,
            "rawid": rawid,
            "create_time": BaseTime().get_beijin_date_strins(format="%Y-%m-%d %H:%M:%S"),
            "update_time": BaseTime().get_beijin_date_strins(format="%Y-%m-%d %H:%M:%S")
        }
        bools, dicts = await SequoiadbHelper.lob_insert(table, datas, msg_tab, sdb_data_msg)
        if bools:
            return_info.status = SUCCESS
            return_info.msg_code = MsgCode.SUCCESS_CODE
            return_info.msg = "lob插入存储成功"
            return_info.data = dicts
        else:
            return_info.status = FAILED
            # 编码都为暂时测试使用, 有需要再封入固定常量中
            return_info.msg_code = 998
            return_info.msg = "lob插入存储失败"
            return_info.data = ""
        return return_info.todict()


@router.post("/se_lob/delete")
async def lob_test(input: InputPlatformModel[LobDelete]):
    return_info = ReturnInfo()
    db_name = Coll.col_db_dicts.get(input.data.table_name, None)
    if db_name is None:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "请在服务端代码Coll中配置集合与数据库的对应关系"
        return_info.data = ""
        return return_info.todict()
    msg_col_name = db_name + "_lob_metadata"
    msg_tab = Coll.get_table_conn(msg_col_name)

    # 两个链接都用同一个链接进行测试
    table = Coll.get_table_conn(input.data.table_name)
    rawid = input.data.rawid
    sdb_data_msg = {
        "clname": input.data.table_name,
        "rawid": rawid
    }
    bools, dicts = await SequoiadbHelper.lob_delete(table, msg_tab, sdb_data_msg)
    if bools:
        return_info.status = SUCCESS
        return_info.msg_code = 200
        return_info.msg = "lob插入存储成功"
        return_info.data = dicts
    else:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "lob插入存储失败"
        return_info.data = ""
    return return_info.todict()


@router.post("/se_lob/replace")
async def lob_test(input: InputPlatformModel[LobReplace]):
    return_info = ReturnInfo()
    table_name = input.data.table_name
    rawid = input.data.rawid
    datas = input.data.datas
    db_name = Coll.col_db_dicts.get(table_name, None)
    if db_name is None:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "请在服务端代码Coll中配置集合与数据库的对应关系"
        return_info.data = ""
        return return_info.todict()
    msg_col_name = db_name + "_lob_metadata"
    msg_tab = Coll.get_table_conn(msg_col_name)
    # 两个链接都用同一个链接进行测试
    table = Coll.get_table_conn(table_name)
    dicts = await NOSQLHelper.find_one(sdb_conn=msg_tab, filter={"clname": table_name, "rawid": rawid})
    if dicts:
        lob_id = dicts["lob_id"]
        condition = {"clname": dicts["clname"], "rawid": dicts["rawid"]}
        bools, dicts = await SequoiadbHelper.replace_lob(table, datas, lob_id, condition, msg_tab)
        return_info.status = SUCCESS
        return_info.msg_code = 200
        return_info.msg = "lob replace 存储成功"
        return_info.data = dicts
        return return_info.todict()
    else:
        dicts["_id"] = str(dicts.get("_id", ""))
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "不存在数据 无法replace"
        return_info.data = ""
        return return_info.todict()



@router.post("/se_lob/query")
async def lob_test(input: InputPlatformModel[LobDelete]):
    return_info = ReturnInfo()
    table_name = input.data.table_name
    rawid = input.data.rawid
    db_name = Coll.col_db_dicts.get(table_name, None)
    if db_name is None:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "请在服务端代码Coll中配置集合与数据库的对应关系"
        return_info.data = ""
        return return_info.todict()
    msg_col_name = db_name + "_lob_metadata"
    msg_tab = Coll.get_table_conn(msg_col_name)
    # 两个链接都用同一个链接进行测试
    table = Coll.get_table_conn(table_name)
    dicts = await NOSQLHelper.find_one(sdb_conn=msg_tab, filter={"clname": table_name, "rawid": rawid})
    if dicts:
        lob_id = dicts["lob_id"]
        result = await SequoiadbHelper.query_lob(table, lob_id)
        return_info.status = SUCCESS
        return_info.msg_code = 200
        return_info.msg = "查询成功"
        return_info.data = result
        return return_info.todict()
    else:
        return_info.status = FAILED
        # 编码都为暂时测试使用, 有需要再封入固定常量中
        return_info.msg_code = 998
        return_info.msg = "不存在数据"
        return_info.data = ""
        return return_info.todict()